import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
// 导出了一个vuex的仓库
import vuexPer from "vuex-persistedstate";
// 引入持久化插件
export default new Vuex.Store({
  state: {
    username: "小明",
    // list: JSON.parse(localStorage.getItem("list")) || [
    //   { name: "张三", age: 19 },
    //   { name: "王昭君", age: 300 },
    //   { name: "小鲁班", age: 6 },
    //   { name: "妲己", age: 3000 },
    //   { name: "杨玉环", age: 1000 },
    // ],
    list: [
      { name: "张三", age: 19 },
      { name: "王昭君", age: 300 },
      { name: "小鲁班", age: 6 },
      { name: "妲己", age: 3000 },
      { name: "杨玉环", age: 1000 },
    ],
  },
  getters: {},
  mutations: {
    remove(state, index) {
      state.list.splice(index, 1);
      // localStorage.setItem("list", JSON.stringify(state.list));
    },
    //mutations方法的第一个参数永远都是state 这是默认的参数 第二个参数才是刚才传来的参数
    add(state, obj) {
      state.list.push(obj);
      // localStorage.setItem("list", JSON.stringify(state.list));
      //存本地存储
    },
    //添加
  },
  actions: {},
  modules: {},
  plugins: [vuexPer()],
});
